import {
  _get
} from '../../api/setting'
import {
  request
} from '../../api/index'
import {
  productList
} from '../../api/setting'


const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    recordState: app.globalData.editState,
    dataSource: [],
    selfId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setPageProperty(options);
    await this.fetchSetting();
    this.fetchData();
  },

  /**
   * 设置页面属性
   */
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '成品指标'
    })
    this.setData({
      id: options.id,
      fromNavigation: options.navigation || 0,
      recordState: app.globalData.editState,
      selfId: options.data_id === 'null' ? null : options.data_id
    })
  },

  toHome() {
    wx.navigateBack({
      delta: 9
    })
  },
  onInputChange(event) {
    const {
      dataSource
    } = this.data, {
      index,
      i
    } = event.currentTarget.dataset;
    dataSource[index]['value'][i].value = event.detail;
    this.setData({
      dataSource
    })
  },
  onSelectChange(event) {
    const {
      dataSource
    } = this.data, {
      index,
      i
    } = event.currentTarget.dataset;
    dataSource[index]['value'][i].value = event.detail[0];
    this.setData({
      dataSource
    })
  },
  fetchData() {
    request.productList({
      collectionRecordIds: this.data.id,
    }, (e) => {
      if (e && e[0]?.jsonData) {
        this.setData({
          dataSource: JSON.parse(e[0]?.jsonData).map(it => {
            return {
              ...it,
              value: it.value
            }
          }).map(it => {
            return {
              ...it,
              value: it.value.map(item => {
                return {
                  ...item,
                  selectOptions: item.selectValues?.map(t => {
                    return {
                      name: t,
                      value: t
                    }
                  })
                }
              })
            }
          })
        })
      }
    })
  },
  async fetchSetting() {
    return new Promise((resolve, reject) => {
      productList({}, res => {
        this.setData({
          dataSource: res.map(it => {
            return {
              ...it,
              key: it.name,
              value: it.settingsValue
            }
          }).map(it => {
            return {
              ...it,
              value: it.value.map(item => {
                return {
                  ...item,
                  selectOptions: item.selectValues?.map(t => {
                    return {
                      name: t,
                      value: t
                    }
                  })
                }
              })
            }
          })
        })
        resolve();
      })
    })
  },

  onChange(event) {

    const dataSource = this.data.dataSource.concat([]).map(it => {
      it.value.forEach(item => {
        delete item.selectOptions;
      })
      return {
        ...it,
        value: it.value
      }
    });
    const obj = {
      collectionRecordId: this.data.id,
      jsonData: JSON.stringify(dataSource),
      id: this.data.selfId
    };

    request.productAdd(obj, () => {
      wx.showModal({
        title: '提示',
        content: '采集记录已完成，是否回到首页？',
        success(res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '../home/index',
            })
          }
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})